﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
	using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.Base;
    using UFIDA.U9.CBO.SCM.Supplier;
    using UFIDA.U9.ISV.Supplier.Proxy;
    using UFSoft.UBF.AopFrame;	
	using UFSoft.UBF.Util.Context;

	/// <summary>
	/// SupplierSaveSV1 partial 
	/// </summary>	
	public partial class SupplierSaveSV1 
	{	
		internal BaseStrategy Select()
		{
			return new SupplierSaveSV1ImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class SupplierSaveSV1ImpementStrategy : BaseStrategy
	{
		public SupplierSaveSV1ImpementStrategy() { }

		public override object Do(object obj)
		{						
			SupplierSaveSV1 bpObj = (SupplierSaveSV1)obj;

            try
            {
                if (bpObj.Data == null || bpObj.Data.Count == 0)
                    throw new Exception("传入参数为空");

                List<SupplierData> retlist = new List<SupplierData>();
                List<UFIDA.U9.ISV.Supplier.SupplierDTOData> inputdtos = new List<UFIDA.U9.ISV.Supplier.SupplierDTOData>();
                List<object> inputlist = DTOUtils.DTOListPropCopy(bpObj.Data.ConvertAll(item => item as object)
                    , typeof(UFIDA.U9.ISV.Supplier.SupplierDTOData));
                foreach (UFIDA.U9.ISV.Supplier.SupplierDTOData dto in inputlist)
                {
                    Supplier supplier = Supplier.Finder.Find($"Code='{dto.Code}' and Org={Context.LoginOrg.ID}");
                    dto.Org = new UFIDA.U9.CBO.Pub.Controller.CommonArchiveDataDTOData
                    {
                        ID = Context.LoginOrg.ID,
                        Code = Context.LoginOrg.Code,
                    };

                    if (supplier == null)
                    {
                        //新增逻辑
                        BatchCreateSupplierByDTOSVProxy proxy = new BatchCreateSupplierByDTOSVProxy();
                        proxy.ContextDTO = U9Utils.CreateContext();
                        proxy.SupplierDTOs = new List<UFIDA.U9.ISV.Supplier.SupplierDTOData>() { dto };
                        List<SupplierData> ret = proxy.Do();
                        if (ret.Count > 0)
                            retlist.Add(ret[0]);
                    }
                    else
                    {
                        //修改逻辑
                        BatchModifySupplierByDTOSVProxy proxy = new BatchModifySupplierByDTOSVProxy();
                        proxy.ContextDTO = U9Utils.CreateContext();
                        proxy.SupplierDTOs = new List<UFIDA.U9.ISV.Supplier.SupplierDTOData> { dto };
                        List<SupplierData> ret = proxy.Do();
                        if (ret.Count > 0)
                            retlist.Add(ret[0]);
                    }
                }
                return ResultUtils.success(bpObj, bpObj.Data, retlist);
            }
            catch (Exception ex)
            {
                return ResultUtils.error(bpObj, bpObj.Data, ex);
            }
        }		
	}

	#endregion
	
	
}